package com.tmpt.controller.action;


import com.opensymphony.xwork2.ActionSupport;
import com.tmpt.dto.*;
import com.tmpt.service.IOperationlogService;
import com.tmpt.service.IOrgService;
import com.tmpt.service.ISecurityV2Service;
import com.tmpt.utils.StringUtils;
import com.tmpt.utils.WebUtils;
import com.tmpt.utils.json.MyJSONArray;
import net.sf.json.util.JSONUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class OrgAction extends ActionSupport{
	private Logger log = Logger.getLogger(this.getClass());
	
	private static final long serialVersionUID = 1L;

	private String companyId;
	private String postId;
	private String id;
	private String name;
	private String optType;

	private String result;
	private List<Map<String,Object>> jsonArrayResult;
	private Map<String,Object> jsonObjectResult;

	@Resource private IOrgService orgService;
	@Resource private IOperationlogService operationlogService;
	private A1OperationlogDto oprDtoBack = new A1OperationlogDto(WebUtils.getRequest(), A1OperationlogDto.UserTypeBack);
	 
	public String queryPostJsonTreeByCompanyId(){
		try{
			System.out.println("=========>companyId:"+companyId);
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();
			A2UserDto userDto = (A2UserDto)session.getAttribute("magSystemDtoSession");
			long userId = userDto.getId();
			System.out.println("=========>userId" + userId);

			Result resultObj = orgService.queryPostJsonTreeByCompanyId(1);
			if(!resultObj.isSuccess()){
				this.result = "数据加载失败";
				return "jsonResult";
			}

			String jsonArrayStr = (String)resultObj.getResultMap().get("jsonArrayStr");
			this.jsonArrayResult = (List<Map<String,Object>>)resultObj.getResultMap().get("list");
			System.out.println("-----" + this.jsonArrayResult.toString());

		}catch (Exception e) {
			this.result = "出错";
			e.printStackTrace();
		}
		return "jsonArray";
	}


	public String removePostNode(){
		Result resultObj = new Result();
		try{
			System.out.println("=========>companyId:"+companyId);
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();
			A2UserDto userDto = (A2UserDto)session.getAttribute("magSystemDtoSession");
			long userId = userDto.getId();
			System.out.println("=========>userId:" + userId);
			System.out.println("=========>postId:" + postId);

			resultObj = orgService.removePostById(Long.parseLong(postId));
		}catch (Exception e) {
			resultObj.fault("出错:"+e.getLocalizedMessage());
			e.printStackTrace();
		}

		this.jsonObjectResult = resultObj.getResultMap();
		return "jsonObject";
	}

	public String addeditPostNode(){
		Result resultObj = new Result();
		try{
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();
			A2UserDto userDto = (A2UserDto)session.getAttribute("magSystemDtoSession");
			long userId = userDto.getId();

			A2PostDto postDto = new A2PostDto();
			postDto.setName(name.trim());
			if("add".equals(optType)){
				//添加
				long parantId = 0;//根节点
				if(!id.toLowerCase().contains("cid")){
					parantId = Long.parseLong(id);
				}
				resultObj = orgService.addPost(postDto,parantId,Long.parseLong(companyId));
			}else if("edit".equals(optType)){
				//修改
				postDto.setId(Long.parseLong(id));
				resultObj = orgService.modifyPostById(postDto);
			}else{
				resultObj.fault("失败：没有相应操作类型");
			}

		}catch (Exception e) {
			resultObj.fault("出错:"+e.getLocalizedMessage());
			e.printStackTrace();
		}

		this.jsonObjectResult = resultObj.getResultMap();
		return "jsonObject";
	}



	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<Map<String, Object>> getJsonArrayResult() {
		return jsonArrayResult;
	}

	public void setJsonArrayResult(List<Map<String, Object>> jsonArrayResult) {
		this.jsonArrayResult = jsonArrayResult;
	}

	public String getPostId() {
		return postId;
	}

	public void setPostId(String postId) {
		this.postId = postId;
	}

	public Map<String, Object> getJsonObjectResult() {
		return jsonObjectResult;
	}

	public void setJsonObjectResult(Map<String, Object> jsonObjectResult) {
		this.jsonObjectResult = jsonObjectResult;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOptType() {
		return optType;
	}

	public void setOptType(String optType) {
		this.optType = optType;
	}
}
